package aiowang.javaprimary.publisherconfirms;

import aiowang.javaprimary.utils.RabbitMqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author aiowang
 * @description:
 * @date 2021/8/26
 **/
public class Sina {

    private static final String QUEUE_SINA = "us";
    private static final String EXCHANGE_WEATHER_TOPIC = "exchange_weather_topic";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitMqUtils.getConnection();
        final Channel channel = connection.createChannel();

        //通道绑定交换机
        /**
         * 参数明细
         * 1、交换机名称
         * 2、交换机类型，fanout、topic、direct、headers
         */
        //Publish/subscribe发布订阅模式
        channel.exchangeDeclare(EXCHANGE_WEATHER_TOPIC, BuiltinExchangeType.TOPIC);

        channel.queueDeclare(QUEUE_SINA, false, false, false, null);
        channel.queueBind(QUEUE_SINA, EXCHANGE_WEATHER_TOPIC, "us.#");

        channel.basicQos(1);
        channel.basicConsume(QUEUE_SINA , false , new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("腾讯天气收到气象信息：" + new String(body));
                channel.basicAck(envelope.getDeliveryTag() , false);
            }
        });
    }

}
